#include"GSQuelchList.h"

GSquelchList::GSquelchList() {
	c = false;
}

void GSquelchList::save(QString fn) {
	if(!changed()) return;
	if(QFile::exists(fn))
		QFile::remove(fn);
	
	QFile out(fn);
	out.open(QIODevice::ReadWrite);
	QTextStream stream(&out);
	
	int length = l.size();
	for(int i=0; i<length; i++) {
		stream<<l[i] <<"\n";
	}
	c = false;
}

void GSquelchList::load(QString fn) {
	l.clear();
	c = false;
	
	QFile in(fn);
	if(!in.open(QIODevice::ReadOnly)) return;
	QTextStream stream(&in);
	
	QString line;
	while(!(line = stream.readLine()).isEmpty())
		l.push_back(line);
}

void GSquelchList::add(QString name) {
	l.push_back(name);
	c = true;
}

void GSquelchList::remove(QString name) {
	l.removeOne(name);
	c = true;
}

bool GSquelchList::notSquelched(QString name) {
	return (l.indexOf(name)==-1?1:0);
}

bool GSquelchList::changed() {
	return c;
}

void GSquelchList::clear() {
	l.clear();
	c = false;
}

QString GSquelchList::listCSV() {
	int length = l.size();
	QString retlist;
	for(int i=0; i<length; i++) {
		retlist.append(l[i]);
		if(i < (length - 1)) retlist.append("`");
	}
	return retlist;
}

int GSquelchList::size() {
	return l.size();
}
